package zhihui.mobile.ui.shop.address;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zgm.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.address.ShoppingAddress;

/**
 * Created by 张公铭 on 2017/8/16 0016.
 */

public class ShopAddressManagerItemAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater = null;
    private List<ShoppingAddress> list = new ArrayList<>();
    private IShopAddressItemListener listener;
    private Drawable drawable = null;
    private Drawable editDrawable = null;


    public interface IShopAddressItemListener
    {
        void delete(ShoppingAddress address);

        void update(ShoppingAddress address);

        void setDefault(ShoppingAddress address, boolean defaultValue);
    }

    public ShopAddressManagerItemAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
        drawable = mContext.getResources().getDrawable(R.drawable.shop_delete);
        drawable.setBounds(0, 0, DensityUtil.dip2px(mContext, 20), DensityUtil.dip2px(mContext, 20));

        editDrawable = mContext.getResources().getDrawable(R.drawable.shop_edit);
        editDrawable.setBounds(0, 0, DensityUtil.dip2px(mContext, 20), DensityUtil.dip2px(mContext, 20));

    }

    public void setList(List<ShoppingAddress> list)
    {
        if (this.list == null)
        {
            this.list = new ArrayList<>();
        }
        this.list.clear();
        this.list.addAll(list);
    }

    public void setListener(IShopAddressItemListener listener)
    {
        this.listener = listener;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv_name, tv_phone, tv_address, tv_status;
        CheckBox cb_select;
        RelativeLayout rl_manager;
        Button btn_delete, btn_edit;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_address_manager_item, null);
            tv_name = (TextView) convertView.findViewById(R.id.tv_shop_address_manager_item_name);
            tv_phone = (TextView) convertView.findViewById(R.id.tv_shop_address_manager_item_phone);
            tv_address = (TextView) convertView.findViewById(R.id.tv_shop_address_manager_item_address);
            tv_status = (TextView) convertView.findViewById(R.id.tv_shop_address_manager_item_status);
            cb_select = (CheckBox) convertView.findViewById(R.id.cb_shop_address_manager_item_default);
            rl_manager = (RelativeLayout) convertView.findViewById(R.id.rl_shop_address_manager_item_manager);
            btn_delete = (Button) convertView.findViewById(R.id.btn_shop_address_manager_item_delete);
            btn_edit = (Button) convertView.findViewById(R.id.btn_shop_address_manager_item_edit);

            ViewCache viewCache = new ViewCache();
            viewCache.tv_name = tv_name;
            viewCache.tv_phone = tv_phone;
            viewCache.tv_address = tv_address;
            viewCache.tv_status = tv_status;
            viewCache.cb_select = cb_select;
            viewCache.rl_manager = rl_manager;
            viewCache.btn_delete = btn_delete;
            viewCache.btn_edit = btn_edit;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            tv_name = viewCache.tv_name;
            tv_phone = viewCache.tv_phone;
            tv_address = viewCache.tv_address;
            tv_status = viewCache.tv_status;
            cb_select = viewCache.cb_select;
            rl_manager = viewCache.rl_manager;
            btn_delete = viewCache.btn_delete;
            btn_edit = viewCache.btn_edit;
        }

        final ShoppingAddress address = list.get(position);
        tv_name.setText(address.getName());
        tv_phone.setText(address.getTel());
        tv_address.setText(address.getAddress());
        if (address.getIsdefault() == 1)
        {
            tv_status.setText("默认地址");
            cb_select.setChecked(true);
        } else
        {
            tv_status.setText("设为默认");
            cb_select.setChecked(false);
        }
        cb_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                listener.setDefault(address, isChecked);
            }
        });

        btn_delete.setCompoundDrawables(drawable, null, null, null);
        btn_delete.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                listener.delete(address);
            }
        });

        btn_edit.setCompoundDrawables(editDrawable, null, null, null);
        btn_edit.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                listener.update(address);
            }
        });

        return convertView;
    }

    public class ViewCache
    {
        public TextView tv_name, tv_phone, tv_address, tv_status;
        public CheckBox cb_select;
        public RelativeLayout rl_manager;
        public Button btn_delete, btn_edit;
    }
}
